package com.tanhua.server.controller;

import com.aliyun.oss.internal.OSSUtils;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UsersControler {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询用户资料
     */
    @GetMapping
    public ResponseEntity users(Long userID) {
        if(userID == null) {
            userID = UserHolder.getUserId();
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

    /**
     * 互相喜欢，喜欢，粉丝 - 统计 --南五磊
     */
    @GetMapping("/counts")
    public ResponseEntity counts(){
        Map counts=userInfoService.counts();
        return ResponseEntity.ok(counts);

    }

    /**
     * 更新用户头像
     * @param headPhoto 用户头像
     * @return
     * @throws IOException 异常
     * 赵旭飞
     */
    @PostMapping("/header")
    public ResponseEntity updateHead(MultipartFile headPhoto) throws IOException {
        //获取用户id
        User user = UserHolder.get();
        //通过用户id修改用户的头像
        userInfoService.updateUserHead(headPhoto,user.getId());

        //修改成功，返回"修改成功"
        return ResponseEntity.ok("修改成功");
    }
    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 -
     * @param page
     * @param pagesize
     * @param type
     * @return
     */
    @GetMapping("/friends/{type}")
    public PageResult friendsType(@PathVariable Integer type,
                                  @RequestParam(defaultValue = "1") Integer page,
                                  @RequestParam(defaultValue = "10") Integer pagesize,
                                  String nickname) {

        PageResult result = userInfoService.friendsType(page, pagesize, nickname, type);
        return result;
    }


    /**
     * 粉丝 - 喜欢
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity fansLike(@PathVariable Long uid) {
        userInfoService.fansLike(uid);
        return ResponseEntity.ok(null);
    }

    /**
     * 粉丝 - 取消
     */
    @DeleteMapping("/like/{uid}")
    public ResponseEntity removeLike(@PathVariable Long uid) {
        userInfoService.removeLike(uid);
        return ResponseEntity.ok(null);
    }

}
